const router = require('express').Router()
const validateToken = require('../../../common/validateToken')
const status_code = require('../../../common/status_code')
const db = require('../../../db/async-db')
const { to } = require('await-to-js')
const Bee = require('../../../common/bee/bee')

// 转为树形结构
const toTree = (list, pid) => {
  const arr = []
  list.forEach((item) => {
    if (item.parent_id === pid) {
      arr.push({
        id: item.id,
        title: item.title,
        description: item.description,
        parent_id: item.parent_id,
        created_time: item.created_time,
        img: item.img,
        url: item.url,
        is_delete: item.is_delete,
        updated_time: item.updated_time,
        children:
          toTree(list, item.id).length > 0 ? toTree(list, item.id) : null,
      })
    }
  })
  return arr
}

router.get('/get-class', async (req, resp) => {
  // 错误处理
  function handleError(errCode, error) {
    resp.json({
      code: errCode,
      msg: status_code[errCode],
    })
    console.error(error)
  }

  const { sqltype, id } = req.query
  if (!Bee.StringUtils.isNotBlank(sqltype))
    return handleError(10003, '参数错误')

  // sqltype: 0 = 只查找启用的栏目  1 = 只查找隐藏的栏目  2 = 查找全部栏目
  let sql = ''
  if (Bee.StringUtils.getStrLast(sqltype, 1) === '0') {
    sql =
      'SELECT id, title, description, created_time, parent_id, img, url, is_delete, updated_time FROM class WHERE is_delete = 0'
    if (id) {
      sql =
        'SELECT id, title, description, created_time, parent_id, img, url, is_delete, updated_time FROM class WHERE is_delete = 0 AND id = ?'
    }
  } else if (Bee.StringUtils.getStrLast(sqltype, 1) === '1') {
    sql =
      'SELECT id, title, description, created_time, parent_id, img, url, is_delete, updated_time FROM class WHERE is_delete = 1'
    if (id) {
      sql =
        'SELECT id, title, description, created_time, parent_id, img, url, is_delete, updated_time FROM class WHERE is_delete = 1 AND id = ?'
    }
  } else if (Bee.StringUtils.getStrLast(sqltype, 1) === '2') {
    sql =
      'SELECT id, title, description, created_time, parent_id, img, url, is_delete, updated_time FROM class'
    if (id) {
      sql =
        'SELECT id, title, description, created_time, parent_id, img, url, is_delete, updated_time FROM class WHERE id = ?'
    }
  } else {
    return handleError(10003, '参数错误')
  }

  if (id) {
    const [getClassError, getClassSuccess] = await to(db.query(sql, [id]))
    if (getClassError || !getClassSuccess) return handleError(-1, getClassError)
    return resp.json({
      code: 0,
      msg: status_code[0],
      data:
        sqltype.length > 1 &&
        !id &&
        Bee.StringUtils.getStrLast(sqltype, 2) === '1'
          ? toTree(getClassSuccess, 0)
          : getClassSuccess,
    })
  } else {
    const [getClassError, getClassSuccess] = await to(db.query(sql))
    if (getClassError || !getClassSuccess) return handleError(-1, getClassError)
    return resp.json({
      code: 0,
      msg: status_code[0],
      data:
        sqltype.length > 1 &&
        !id &&
        Bee.StringUtils.getStrLast(sqltype, 2) === '1'
          ? toTree(getClassSuccess, 0)
          : getClassSuccess,
    })
  }
})

module.exports = router
